//
//  AddClientController.m
//  quickBatchManager
//
//  Created by 王定波 on 15/10/12.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "AddClientController.h"

@interface AddClientController ()
{
     AAMenuBubbles *payMenu;
    int saleType;
}

@end

@implementation AddClientController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)initUI
{
    _addressTextView.layer.borderColor=[UIColor colorWithWhite:0.85 alpha:1].CGColor;
    _addressTextView.layer.borderWidth=0.5;
    _addressTextView.layer.cornerRadius=5;
    _addressTextView.clipsToBounds=YES;
    
    
    UIImageView *lv=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    lv.contentMode=UIViewContentModeCenter;
    [lv setImage:[UIImage imageNamed:@"yuan"]];
    _priceField.rightView=lv;
    _priceField.rightViewMode = UITextFieldViewModeAlways;
    NSArray *paytype=@[@"售价",@"进价",@"折扣"];
    
    [_saleTypeBtn bk_whenTapped:^{
        if(payMenu) {
            payMenu = nil;
        }
        payMenu = [[AAMenuBubbles alloc] initWithPoint:_saleTypeBtn.center radius:70 andBubbles:paytype inView:self.view];
        payMenu.bubbleRadius = 30;
        payMenu.delegate=self;
        [payMenu show];
    }];
    
    
    [_submitBtn bk_whenTapped:^{
        if([_nameField.text isBlank])
        {
            [self showAlert:@"姓名不能为空"];
            return;
        }
        
        ClientModel *client=[[ClientModel alloc] init];
        client.clientName=_nameField.text;
        client.mobile=_phoneField.text;
        client.area=_areaField.text;
        client.address=_addressTextView.text;
        client.saleValue=[_priceField.text doubleValue];
        client.saleType=saleType;
        client.clientType=(_clientType==AccountTypeClient?0:1);
        
        if(!_client)
        [ClientModel insertToDB:client];
        else  {
            client.clientId=_client.clientId;
            [client updateToDB];
        }
        
        
        if([_delegate respondsToSelector:@selector(didChange:andData:)])
        {
            [_delegate didChange:ChangeTypeClient andData:nil];
        }
        
        NSMutableArray *navarray=[[NSMutableArray alloc] init];
        [navarray addObject:[self.splitViewController.viewControllers objectAtIndex:0] ];
        self.splitViewController.viewControllers=navarray;
        
    }];
    
    
    
    
    
    if(_client)
    {
        _nameField.text=_client.clientName;
        _phoneField.text=_client.mobile;
        _areaField.text=_client.area;
        _addressTextView.text=_client.address;
        _priceField.text=[ToolUtils priceToStr:_client.saleValue];
        saleType=_client.saleType;
        [_saleTypeBtn setTitle:[paytype objectAtIndex:saleType] forState:UIControlStateNormal];
    }
    

}


-(void)AAMenuBubbles:(AAMenuBubbles *)shareBubbles andIndex:(int)index
{
    saleType=index;
    [_saleTypeBtn setTitle:[shareBubbles.bubblestrs objectAtIndex:index] forState:UIControlStateNormal];
}

@end
